package xyz.jcat.sat.oauth2.server.client;

import cn.dev33.satoken.oauth2.model.SaClientModel;
import lombok.Getter;
import lombok.Setter;
import xyz.jcat.common.enums.YesNoEnum;

import java.util.Date;
import java.util.Objects;

@Getter
@Setter
public class Oauth2Client {

    private String clientId;
    private String clientSecret;
    //是否在每次 Refresh-Token 刷新 Access-Token 时，产生一个新的 Refresh-Token
    private Integer isNewRefresh;
    private Long accessTokenTimeout;
    private Long refreshTokenTimeout;
    private Date createTime;

    public boolean isNewRefresh() {
        return YesNoEnum.yes(this.isNewRefresh);
    }

    public static SaClientModel toSaClientModel(Oauth2Client oauth2Client) {
        if(Objects.isNull(oauth2Client)) {
            return null;
        }
        return new SaClientModel()
                .setClientId(oauth2Client.getClientId())
                .setClientSecret(oauth2Client.getClientSecret())
                .setIsNewRefresh(oauth2Client.isNewRefresh())
                .setAccessTokenTimeout(oauth2Client.getAccessTokenTimeout())
                .setRefreshTokenTimeout(oauth2Client.getRefreshTokenTimeout());
    }
}
